package com.slmxz.tms.document.controller;

import com.slmxz.tms.common.model.Query;
import com.slmxz.tms.common.result.PageResult;
import com.slmxz.tms.common.result.Result;
import com.slmxz.tms.common.support.Condition;
import com.slmxz.tms.document.model.entity.Route;
import com.slmxz.tms.document.model.vo.RouteVO;
import com.slmxz.tms.document.service.RouteService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 线路前端控制层
 * @author sunyao
 * @Date 2024-11-15
 */
@Tag(name = "线路接口")
@RestController
@RequestMapping("/document/route")
@RequiredArgsConstructor
public class RouteController  {

    private final RouteService routeService;

    @Operation(summary = "线路分页列表")
    @GetMapping("/page")
    public PageResult<RouteVO> selectRoutePage(Query query, RouteVO route) {
        IPage<RouteVO> result = this.routeService
                .selectRoutePage(Condition.getPage(query),route);
        return PageResult.success(result);
    }

    @Operation(summary = "新增线路")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('document:route:add')")
    public Result<Void> saveRoute(@RequestBody @Valid RouteVO  route) {
        boolean result = this.routeService.saveRoute(route);
        return Result.result(result);
    }

    @Operation(summary = "获取线路详情数据")
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPerm('document:route:edit')")
    public Result<Route> selectRouteDetail(
        @Parameter(description = "线路ID") @PathVariable Long id
    ) {
        Route route = this.routeService.selectRouteDetail(id);
        return Result.success(route);
    }

    @Operation(summary = "修改线路")
    @PutMapping(value = "/update")
    @PreAuthorize("@ss.hasPerm('document:route:edit')")
    public Result<Void> updateRoute(
            @RequestBody @Validated RouteVO route
    ) {
        boolean result = routeService.updateRoute(route);
        return Result.result(result);
    }

    @Operation(summary = "删除线路")
    @DeleteMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPerm('document:route:delete')")
    public Result<Void> deleteRoutes(
        @Parameter(description = "线路ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = this.routeService.deleteRoutes(ids);
        return Result.result(result);
    }

    @Operation(summary = "激活或者注销线路")
    @PostMapping("/activeOrLogout")
    @PreAuthorize("@ss.hasPerm('document:business:delete')")
    public Result<Void> activeOrLogout(@RequestBody RouteVO route) {
        boolean result = this.routeService.activeOrLogout(route);
        return Result.result(result);
    }
}
